package com.kly.juc.completablefuture;

import java.util.concurrent.*;

/**
 * @author admin
 * @date 2025/4/19
 */
public class CompletableFutureDemo {
    public static void main(String[] args) {
        test1();
    }

    public static void test1() {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                5,
                10,
                1,
                TimeUnit.MINUTES,
                new ArrayBlockingQueue<>(100)
        );
        CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return "hello 1";
        }, threadPoolExecutor);
        CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return "hello 2";
        }, threadPoolExecutor);

        try {
            String s = future1.getNow("not exist!");
            System.out.println(s);
            String s1 = future2.join();
            System.out.println(s1);
            String s2 = future2.get();
            System.out.println(s2);
        } catch (Exception e) {
            System.out.println(e);
        }

        threadPoolExecutor.shutdown();


    }
}
